package cn.pop.hsp_system_v2.Controller;

import cn.pop.hsp_system_v2.Mapper.CardMapper;
import cn.pop.hsp_system_v2.Mapper.PatientMapper;
import cn.pop.hsp_system_v2.Pojo.Appointment;
import cn.pop.hsp_system_v2.Pojo.OutPatient;
import cn.pop.hsp_system_v2.Pojo.Patient;
import cn.pop.hsp_system_v2.Pojo.Scheduling;
import cn.pop.hsp_system_v2.Service.ICardService;
import cn.pop.hsp_system_v2.Service.IPatientService;
import cn.pop.hsp_system_v2.Service.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;


@Controller
public class CardContorller {
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private SchedulingService schedulingService;

    @GetMapping("/bindCard")
    public String toBindCard(HttpSession session, Model model){
        StringBuilder stringBuilder=new StringBuilder();
        for (int i=0;i<11;i++){
            double random = (Math.random()*10);
            stringBuilder.append((int)random);
        }
        String s= String.valueOf(stringBuilder);
        model.addAttribute("uuid",s);
        Patient patient=new Patient();
        patient= (Patient) session.getAttribute("user");
        System.out.println(patient);
        return "bindCard.html";
    }

    @PostMapping("/bindMCard")
    public String bindMCard(@RequestParam(value = "uuid") String uuid,
                           @RequestParam(value = "name",required = false)String name,
                           @RequestParam(value = "tel",required = false)String tel,
                           @RequestParam(value = "id",required = false)String id,
                           @RequestParam(value = "gender",required = false)Integer gender,
                           @RequestParam(value = "age",required = false)Integer age,
                           HttpSession session,Model model){
        cardMapper.bindCard(uuid,0);
        System.out.println(gender);
//        if (birthday!=null&&birthday.length()!=0){
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            java.util.Date d=null;
//            try {
//                d =formatter.parse(birthday);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            java.sql.Date date=new java.sql.Date(d.getTime());
//            System.out.println(date);
//        }
        Patient patient=new Patient();
        patient.setUuid(uuid);
        patient.setName(name);
        patient.setIdCard(id);
        patient.setPhone(Long.valueOf(tel));
        patient.setAge(age);
        patient.setSex(gender);
        Long userid= (Long) session.getAttribute("userid");
        patient.setUserId(userid);
        iPatientService.insertPatient(patient);

        Collection<OutPatient> outPatients= iPatientService.selectAllOutPatient();
        model.addAttribute("outpatients",outPatients);
        return "patient/department";
    }

    @PostMapping("/bindCard")
    public String bindCard(@RequestParam(value = "uuid") String uuid,
                           @RequestParam(value = "name",required = false)String name,
                           @RequestParam(value = "tel",required = false)String tel,
                           @RequestParam(value = "id",required = false)String id,
                           @RequestParam(value = "gender",required = false)Integer gender,
                           @RequestParam(value = "birthday",required = false)String birthday,
                           HttpSession session,Model model){
        cardMapper.bindCard(uuid,1);
        System.out.println(gender);
//        if (birthday!=null&&birthday.length()!=0){
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            java.util.Date d=null;
//            try {
//                d =formatter.parse(birthday);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            java.sql.Date date=new java.sql.Date(d.getTime());
//            System.out.println(date);
//        }
        Patient patient=new Patient();
        patient.setUuid(uuid);
        patient.setName(name);
        patient.setIdCard(id);
        patient.setPhone(Long.valueOf(tel));
        patient.setSex(gender);
        Long userid= (Long) session.getAttribute("userid");
        patient.setUserId(userid);
        iPatientService.insertPatient(patient);

        Collection<OutPatient> outPatients= iPatientService.selectAllOutPatient();
        model.addAttribute("outpatients",outPatients);
        return "patient/department";
    }

    @GetMapping("/userCenter")
    public String toUserCenter(){
        return "userCenter.html";
    }

    @GetMapping("/getRemainNumber")
    @ResponseBody
    public Scheduling getRemainNumber(Integer schedulingId){
        Scheduling scheduling=schedulingService.findSchedulingById(schedulingId);
        System.out.println(scheduling);
        return scheduling;
    }

}
